package com.polaris.live.popup.party

import android.content.Context
import android.os.CountDownTimer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.PartyGamePlayAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogPartyGameBalanceBinding
import com.polaris.live.interface_bag.XpopupCallBackImpl
import com.polaris.live.resp.back_resp.PartyGameSettlementResp
import com.polaris.live.translation.R.string

/**
 * 派对游戏结算
 * @author Created by 天晴 on 2024/3/15 19:13
 * @since 1.0.0
 **/
class PartyGameBalancePopup(
    context: Context,
    private val mCountdown: Long,
    private val mGameResp: List<PartyGameSettlementResp>,
    private val onNext: () -> Unit,
    private val onJoin: () -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_party_game_balance
    }

    private lateinit var mBinding: DialogPartyGameBalanceBinding

    private val mTime: TimeCount by lazy {
        TimeCount(mCountdown * 1000, 1000)
    }

    private val mAdapter by lazy { PartyGamePlayAdapter() }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogPartyGameBalanceBinding.bind(popupImplView)
        val meData = mGameResp.find { it.base.userId == UserManager.getUserId() }
        val haveMe = meData != null


        if (mCountdown > 0) {
            mTime.start()
        }
        mBinding.apply {
            if (meData != null) {
                val bgResource = if (meData.winner) {
                    Pair(R.drawable.party_bg_win, R.drawable.r12_game_balance_win)
                } else {
                    Pair(R.drawable.party_bg_loss, R.drawable.r12_game_balance_loss)
                }
                titleBg.setImageResource(bgResource.first)
                bgRl.setBackgroundResource(bgResource.second)
            } else {
                titleBg.setImageResource(R.drawable.party_bg_end)
                bgRl.setBackgroundResource(R.drawable.r12_game_balance_win)
            }

            cancel.setOnSingleClickListener {
                dismiss()
                if (haveMe) {
                    onNext.invoke()
                } else {
                    onJoin.invoke()
                }
            }
            cancel.text =
                if (haveMe) StringUtils.getString(string.play_again) else StringUtils.getString(
                    string.join_game
                )
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                adapter = mAdapter
            }
            mAdapter.addData(mGameResp)
        }
    }

    private inner class TimeCount(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {

        override fun onTick(millisUntilFinished: Long) {
            mBinding.countdownTv.text =
                StringUtils.getString(string.countdown_tips, millisUntilFinished / 1000)
        }

        override fun onFinish() {
            dismiss()
        }

    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun onDestroy() {
        super.onDestroy()
        mTime.cancel()
    }

    companion object {

        fun show(
            context: Context,
            mCountdown: Long,
            gameResp: List<PartyGameSettlementResp>,
            onNext: () -> Unit,
            onJoin: () -> Unit,
            onDismiss: (() -> Unit)?,
        ) {
            //存弹窗时间
            CacheUtil.setLastGameWinPopTime(CacheUtil.getServerTime())
            XPopup.Builder(context)
                .hasNavigationBar(false)
                .isDestroyOnDismiss(true)
                .setPopupCallback(object : XpopupCallBackImpl {
                    override fun onDismiss(popupView: BasePopupView) {
                        onDismiss?.invoke()
                        super.onDismiss(popupView)
                    }
                })
                .asCustom(PartyGameBalancePopup(context, mCountdown, gameResp, onNext, onJoin))
                .show()
        }
    }
}